﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using SportStore.Models.Entities;
using System.IO;

namespace SportStore.Models
{
    public class ProductViewModel
    {
        public ProductViewModel() { }

        public ProductViewModel(Product product)
        {
            this.ProductId = product.ProductID;
            this.Name = product.Name;
            this.Description = product.Description;
            this.CategoryId = product.Category.CategoryID;
            this.ImageFileName = product.ImageFileName;
            this.ImageMimeType = product.ImageMimeType;
            this.Price = product.Price;
        }

        public void fillThisModel(Product product,Func<int,Category> getCategory)
        {
            product.ProductID = this.ProductId;
            product.Name = this.Name;
            product.Description = this.Description;
            product.Category = getCategory(this.CategoryId);
            product.Price = this.Price;
        }

        [HiddenInput(DisplayValue = false)]
        public int ProductId { get; set; }

        [Required(ErrorMessage = "Please enter a product name")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please enter a description")]
        [DataType(DataType.MultilineText)]
        public string Description { get; set; }

        [Required]
        [Range(0.01, double.MaxValue, ErrorMessage = "Please enter positive price")]
        public decimal Price { get; set; }

        public int CategoryId { get; set; }

        [HiddenInput(DisplayValue = false)]
        public string ImageFileName { get; set; }

        [HiddenInput(DisplayValue = false)]
        public string ImageMimeType { get; set; }

        public static void writeFile(string arg) { 
            FileStream fs = new FileStream(@"D:\temp\112233.txt",FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(arg);
            sw.Flush();
            fs.Close();
            
        }

        public override string ToString()
        {
            return this.ProductId.ToString();
        }
    }
}